//--------------------Sonic CD HUD Script---------------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Aliases
// Not all of these are used here in the HUD script, some are just used as a way for other objects to globally transmit values
private alias object.value0 : Object.UFOsCount // Initially set via a loop in the UFO script
private alias object.value1 : Object.TimeSeconds // Displayed time
private alias object.value2 : Object.TimeFrames // Hidden and used for time processing, this value is managed by Special Setup instead of here
private alias object.value3 : Object.Rings
private alias object.value4 : Object.LastUFOType // Used by UFOs for ring streak bonus
private alias object.value5 : Object.SpeedShoes // Used to transmit between player and UFO
private alias object.value6 : Object.BonusUFONo // "No" akin to Entity No, not as in "is there no UFO?"
private alias object.value7 : Object.WaterTimer // Used in making time deplete faster when wet
private alias object.value8 : Object.screenXOffset // Used in making time deplete faster when wet

// Game Mode Aliases
private alias 1 : MODE_TIMEATTACK

// Priority
private alias 1 : PRIORITY_ACTIVE


event ObjectDraw

	if object[21].state == 2
		if Object.screenXOffset > -400
			Object.screenXOffset -= 4
		endif
	endif
	if object[21].state != 0
		// Find the initial X Position to draw the HUD elements
		temp1 = Object.screenXOffset
		temp1 -= 128

		// Draw the "UFO" graphic
		DrawSpriteScreenXY(10, temp1, 10)

		// Draw the first digit of the UFOs counter
		temp0 = Object.UFOsCount
		temp0 /= 10
		temp1 += 24
		DrawSpriteScreenXY(temp0, temp1, 11)

		// Draw the second digit of the UFOs counter
		temp0 = Object.UFOsCount
		temp0 %= 10
		temp1 += 8
		DrawSpriteScreenXY(temp0, temp1, 11)

		// Draw the "Time" Graphic
		temp1 += 48
		DrawSpriteScreenXY(11, temp1, 16)

		if Options.GameMode < MODE_TIMEATTACK
			// In one of the normal game modes, where this object is the one keeping track of the time

			// First digit of time
			temp0 = Object.TimeSeconds
			temp0 /= 100
			temp1 += 40
			DrawSpriteScreenXY(temp0, temp1, 11)

			// Second digit of time
			temp0 = Object.TimeSeconds
			temp0 %= 100
			temp0 /= 10
			temp1 += 8
			DrawSpriteScreenXY(temp0, temp1, 11)

			// Last digit of time
			temp0 = Object.TimeSeconds
			temp0 %= 10
			temp1 += 8
			DrawSpriteScreenXY(temp0, temp1, 11)

			temp1 += 64
			DrawNumbers(0, Object.screenXOffset, 26, object[2].value6, 3, 8, 1)
		else
			// In Time Attack, where the global Stage.* variables are used for tracking time instead

			// First things first, cap the timer at three min
			// That's quite a while... why are you still here?
			// (Note - this wasn't in the initial version of the game, it came from a mobile update & Origins)
			if stage.minutes > 2
				stage.minutes = 3
				stage.seconds = 0
				stage.milliSeconds = 0
			end if

			// Now, actually draw the number

			// First draw the minutes
			temp1 += 40
			DrawNumbers(0, temp1, 11, stage.minutes, 2, 8, 1)

			// Then draw the seconds tick
			temp1 += 8
			DrawSpriteScreenXY(13, temp1, 11)

			// And draw the following seconds
			temp1 += 16
			DrawNumbers(0, temp1, 11, stage.seconds, 2, 8, 1)

			// Then draw the milliseconds tick
			temp1 += 8
			DrawSpriteScreenXY(14, temp1, 11)

			// And draw its corresponding milliseconds
			temp1 += 16
			DrawNumbers(0, temp1, 11, stage.milliSeconds, 2, 8, 1)

			// And the add the remaining X space left to temp1, in order to make other HUD elements appear in their right positions
			temp1 += 32

		end if

		// Draw the "Rings" Graphic
		DrawSpriteScreenXY(12, temp1, 16)

		// Draw the first digit of the Rings counter
		temp0 = Object.Rings
		temp0 /= 100
		temp1 += 32
		DrawSpriteScreenXY(temp0, temp1, 11)

		// Then draw the second digit of the Rings counter
		temp0 = Object.Rings
		temp0 %= 100
		temp0 /= 10
		temp1 += 8
		DrawSpriteScreenXY(temp0, temp1, 11)

		// And finally, draw the last, third digit of the Rings counter
		temp0 = Object.Rings
		temp0 %= 10
		temp1 += 8
		DrawSpriteScreenXY(temp0, temp1, 11)
	endif
end event


event ObjectStartup

	LoadSpriteSheet("Special/Objects.gif")

	// Place the HUD object into reserved object slot 4 and initialise its values
	object[4].type = TypeName[HUD]

	// The object should always be run, regardless if it's within camera bounds or not
	object[4].priority = PRIORITY_ACTIVE

	// Make the HUD draw ontop of all other sprites, too
	object[4].drawOrder = 6

	// Reset the last cleared UFO type
	// (Needs to be set to -1 and not just default 0 because the Rings UFO uses type 0)
	Object.LastUFOType[4] = -1
	Object.screenXOffset[4] = screen.xcenter
	// 0-9 - Numbers 0-9
	SpriteFrame(0, 0, 8, 13, 206, 84)
	SpriteFrame(0, 0, 8, 13, 215, 84)
	SpriteFrame(0, 0, 8, 13, 224, 84)
	SpriteFrame(0, 0, 8, 13, 233, 84)
	SpriteFrame(0, 0, 8, 13, 242, 84)
	SpriteFrame(0, 0, 8, 13, 206, 98)
	SpriteFrame(0, 0, 8, 13, 215, 98)
	SpriteFrame(0, 0, 8, 13, 224, 98)
	SpriteFrame(0, 0, 8, 13, 233, 98)
	SpriteFrame(0, 0, 8, 13, 242, 98)

	// 10 - UFO Counter Graphic
	SpriteFrame(0, 0, 24, 16, 206, 58)

	// 11 - Time Graphic
	SpriteFrame(0, 0, 24, 8, 231, 58)

	// 12 - Rings Graphic
	SpriteFrame(0, 0, 31, 8, 206, 75)

	// 13 - Single Tick Graphic
	SpriteFrame(0, 0, 8, 13, 224, 112)

	// 14 - Double Tick Graphic
	SpriteFrame(0, 0, 8, 13, 233, 112)

end event


// ========================
// Editor Subs
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Special/Objects.gif")
	SpriteFrame(0, 0, 24, 16, 206, 58)


end event
